package com.victor.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.victor.entity.News;
import com.victor.service.NewsService;
import com.victor.utils.FileUpload;
import com.victor.utils.Msg;

/**
 * 新闻管理
 * 
 * @author Mr.Zhang
 *
 */
@RestController
@RequestMapping("/news")
public class NewsController {

	@Autowired
	private NewsService newsService;

	// 获取新闻列表
	@RequestMapping("/getAll")
	public Msg getNewsList() {
		List<News> newss = newsService.getNewsList();
		return Msg.success().add("newss", newss);
	}

	// 新建新闻
	@RequestMapping("/build")
	public Msg build(News news, MultipartFile n_picture_file, HttpServletRequest request) throws Exception {
		if (n_picture_file != null) {
			String n_picture = FileUpload.uploadFile(n_picture_file, request);
			news.setN_picture(n_picture);
		}
		boolean insResult = newsService.build(news);
		return insResult ? Msg.success() : Msg.fail();
	}

	// 修改新闻信息
	@RequestMapping("/update")
	public Msg update(News news, MultipartFile n_picture_file, HttpServletRequest request) throws Exception {
		if (n_picture_file != null) {
			String n_picture = FileUpload.uploadFile(n_picture_file, request);
			news.setN_picture(n_picture);
		}
		boolean updResult = newsService.update(news);
		return updResult ? Msg.success() : Msg.fail();
	}

	// 批量删除
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		boolean delResult = newsService.batchDelete(ids);
		return delResult ? Msg.success() : Msg.fail();
	}
}
